package de.unidue.langtech.teaching.pp.example;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import de.unidue.langtech.teaching.pp.type.DetectedSentimentScore;
import de.unidue.langtech.teaching.pp.type.GivenSentiment;

public class EvaluatorExample
    extends JCasAnnotator_ImplBase
{

    private float correct;
    private float nrOfDocuments;
    
    /* 
     * This is called BEFORE any documents are processed.
     */
    @Override
    public void initialize(UimaContext context)
        throws ResourceInitializationException
    {
        super.initialize(context);
        correct = 0;
        nrOfDocuments = 0;
    }
    
    
    /* 
     * This is called ONCE for each document
     */
    @Override
    public void process(JCas jcas)
        throws AnalysisEngineProcessException
    {
        nrOfDocuments++; 
        
        // get annotations
        GivenSentiment gst = JCasUtil.selectSingle(jcas, GivenSentiment.class);
        DetectedSentimentScore scoreAnno = JCasUtil.selectSingle(jcas, DetectedSentimentScore.class);
        
        // get annotated sentiment-score
        Integer score = scoreAnno.getScore();
        
        // if sentiment-score < 0, emotion=negative, if == 0, emotion = neutral, if score > 0: emotion positive SOURCE http://en.wikipedia.org/wiki/%3F:#Java
        String emotion = score > 0 ? "positive" : (score < 0 ? "negative" : "neutral");        
        
        
        // get emotion-string from annotation
        String givenEmotion = gst.getEmotion();
        
        // if given is objective or "objective-or-neutral" set it to neutral
        if(givenEmotion.startsWith("objective"))
        	givenEmotion = "neutral";
        
        // check if string score-interpretation 
        if(emotion.equals(givenEmotion))
        	correct++;
    }


    /* 
     * This is called AFTER all documents have been processed.
     */
    @Override
    public void collectionProcessComplete()
        throws AnalysisEngineProcessException
    {
        super.collectionProcessComplete();
        
        System.out.println(correct + " out of " + nrOfDocuments + " are correct.");
        System.out.println((correct / nrOfDocuments) *100 + " are correct.");
    } }
